//
//  DDLoginView.m
//  练习
//
//  Created by rimi on 16/11/2.
//  Copyright © 2016年 rimi. All rights reserved.
//

#import "DDLoginView.h"
#import "DDLogInTransitions.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeigh [UIScreen mainScreen].bounds.size.height
#define rectLeftArm CGRectMake(1, 0.122*kHeigh, 40, 65)
#define rectRightArm CGRectMake(self.owlHeadImageView.frame.size.width / 2 + 60, 0.122*kHeigh, 40, 65)
#define rectLeftHand CGRectMake(kWidth/ 2 - 100, self.loginView.frame.origin.y - 22, 50, 40)
#define rectRightHand CGRectMake(kWidth/ 2 + 62, self.loginView.frame.origin.y - 22, 50, 40)
#pragma mark 点击文本输入框猫头鹰显示动画的枚举
typedef NS_ENUM(NSInteger,ClickType){
    clicktypeNone,
    clicktypeUser,
    clicktypePass
};

@interface DDLoginView()<NSCopying,UITextFieldDelegate>
@property (nonatomic,strong) UIImageView* leftHandImageView;//左手
@property (nonatomic,strong) UIImageView* rightHandImageView;//右手
@property (nonatomic,strong) UIImageView* leftArmImageView;//左手臂
@property (nonatomic,strong) UIImageView* rightArmImageView;//右手臂
@property (nonatomic,strong) UIImageView* owlHeadImageView;//猫头鹰
@property (nonatomic,strong) UIView* loginView;//登录界面视图

@property (assign,nonatomic) ClickType clicktype;




@end
@implementation DDLoginView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self layoutUI];
    }
    return self;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.passwordTextFiled resignFirstResponder];
    [self.accountTextField resignFirstResponder];
}
#pragma mark 常用方法
//手臂偏移到蒙住眼睛的位置
- (void)coverOwlEyes{
    [UIView animateWithDuration:0.5 animations:^{
        self.leftArmImageView.frame = CGRectMake(self.leftArmImageView.frame.origin.x + 60, self.leftArmImageView.frame.origin.y - 30, self.leftArmImageView.frame.size.width, self.leftArmImageView.frame.size.height);
        
        self.rightArmImageView.frame = CGRectMake(self.rightArmImageView.frame.origin.x - 48, self.rightArmImageView.frame.origin.y - 30, self.rightArmImageView.frame.size.width, self.rightArmImageView.frame.size.height);
        
        self.leftHandImageView.frame = CGRectMake(self.leftHandImageView.frame.origin.x + 70, self.leftHandImageView.frame.origin.y, 0, 0);
        self.rightHandImageView.frame = CGRectMake(self.rightHandImageView.frame.origin.x - 30, self.rightHandImageView.frame.origin.y, 0, 0);
    } completion:^(BOOL finished) {
        
    }];

    
}
//两个手臂回到初始化状态
- (void)notCoverOwlEyes{
    [UIView animateWithDuration:0.5 animations:^{
        self.leftArmImageView.frame = CGRectMake(self.leftArmImageView.frame.origin.x - 60, self.leftArmImageView.frame.origin.y + 30, self.leftArmImageView.frame.size.width, self.leftArmImageView.frame.size.height);
        
        self.rightArmImageView.frame = CGRectMake(self.rightArmImageView.frame.origin.x+48, self.rightArmImageView.frame.origin.y + 30, self.rightArmImageView.frame.size.width, self.rightArmImageView.frame.size.height);
        
        self.leftHandImageView.frame = CGRectMake(self.leftHandImageView.frame.origin.x-70, self.leftHandImageView.frame.origin.y, 40, 40);
        self.rightHandImageView.frame = CGRectMake(self.rightHandImageView.frame.origin.x +30, self.rightHandImageView.frame.origin.y, 40, 40);
    } completion:^(BOOL finished) {
        
    }];


}
#pragma mark  单例初始化方法
static  DDLoginView* instance = nil;
+ (instancetype)sharedDDLoginView{
   
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[DDLoginView alloc]init];
    });
    return instance;
}

+ (instancetype)allocWithZone:(struct _NSZone *)zone{
    @synchronized (self) {
        if (!instance){
            instance = [super allocWithZone:zone];
        }
    }
    return instance;

}
- (id)copyWithZone:(NSZone *)zone{
    return self;
}
#pragma mark 内部逻辑操作
#pragma mark 布局UI
- (void)layoutUI{
    self.owlHeadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kWidth/ 2 - 211 / 2, 0.136*kHeigh, 211, 0.148*kHeigh)];
    
    self.owlHeadImageView.image=[UIImage imageNamed:@"owlHead"];
    [self addSubview:self.owlHeadImageView];
    
    self.leftArmImageView=[[UIImageView alloc]initWithFrame:rectLeftArm];
    self.leftArmImageView.image=[UIImage imageNamed:@"owlLeftArm"];
    [self.owlHeadImageView addSubview:self.leftArmImageView];
    
    
    self.rightArmImageView = [[UIImageView alloc]initWithFrame:rectRightArm];
    
    self.rightArmImageView.image=[UIImage imageNamed:@"owlRightArm"];
    [self.owlHeadImageView addSubview:self.rightArmImageView];
    
    
    self.loginView=[[UIView alloc]initWithFrame:CGRectMake(15, kHeigh*0.27, kWidth-30, kHeigh*0.29)];
    self.loginView.layer.borderWidth=1;
    self.loginView.layer.borderColor=[UIColor lightGrayColor].CGColor;
    self.loginView.backgroundColor=[UIColor colorWithWhite:0.9 alpha:1];
    [self addSubview:self.loginView];
    
    self.leftHandImageView = [[UIImageView alloc]initWithFrame:rectLeftHand];
    self.leftHandImageView.image = [UIImage imageNamed:@"hand"];
    [self addSubview:self.leftHandImageView];
    
    self.rightHandImageView = [[UIImageView alloc]initWithFrame:rectRightHand];
    self.rightHandImageView.image = [UIImage imageNamed:@"hand"];
    [self addSubview:self.rightHandImageView];
    
   self.accountTextField=[[UITextField alloc]initWithFrame:CGRectMake(30, self.loginView.frame.size.height* 0.14, kWidth-90, self.loginView.frame.size.height*0.2)];
    self.accountTextField.delegate=self;
    self.accountTextField.layer.cornerRadius = 5;
    self.accountTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.accountTextField.layer.borderWidth=0.7;
    self.accountTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 44, self.loginView.frame.size.height*0.2)];
    self.accountTextField.leftViewMode = UITextFieldViewModeAlways;
    self.accountTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.accountTextField.keyboardType = UIKeyboardTypeAlphabet;
    self.accountTextField.placeholder = @"请输入账号";
    UIImageView *userimg=[[UIImageView alloc]initWithFrame:CGRectMake(11, 11, 0.053*kWidth, self.loginView.frame.size.height*0.1)];
    userimg.image=[UIImage imageNamed:@"account"];
    [self.accountTextField.leftView addSubview:userimg];
    [self.loginView addSubview:self.accountTextField];
    
    
    
    self.passwordTextFiled = [[UITextField alloc] initWithFrame:CGRectMake(30, self.loginView.frame.size.height*0.42,kWidth-90, self.loginView.frame.size.height*0.2)];
    self.passwordTextFiled.delegate = self;
    self.passwordTextFiled.layer.cornerRadius = 5;
    self.passwordTextFiled.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    self.passwordTextFiled.layer.borderWidth = 0.5;
    self.passwordTextFiled.secureTextEntry = YES;
    self.passwordTextFiled.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, self.loginView.frame.size.height*0.2)];
    self.passwordTextFiled.leftViewMode = UITextFieldViewModeAlways;
    self.passwordTextFiled.keyboardType = UIKeyboardTypeASCIICapable;
    self.passwordTextFiled.placeholder = @"请输入密码";
    UIImageView* pssimag = [[UIImageView alloc] initWithFrame:CGRectMake(11, 11, 0.053*kWidth, self.loginView.frame.size.height*0.1)];
    pssimag.image = [UIImage imageNamed:@"password"];
    [self.passwordTextFiled.leftView addSubview:pssimag];
    [self.loginView addSubview:self.passwordTextFiled];
    [self createPresentControllerButton: CGRectMake(30, self.loginView.frame.size.height*0.7, kWidth-90, self.loginView.frame.size.height*0.2) btnBgColor:[UIColor blackColor] btnAlpha:1];
    
}
- (void)createPresentControllerButton:(CGRect)frame btnBgColor:(UIColor*)btnBgColor btnAlpha:(CGFloat)btnAlpha{
    self.loginBtn = [[DDLoglnButton alloc]initWithFrame:frame];
    [self.loginBtn setBackgroundColor:btnBgColor];
    self.loginBtn.alpha = btnAlpha;
    [self.loginBtn setTitle:@"登录" forState:(UIControlStateNormal)];
    [self.loginView addSubview:self.loginBtn];
    [self.loginBtn addTarget:self action:@selector(loginBtnIsClicked) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark 文本输入框代理方法
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    [textField resignFirstResponder];
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.accountTextField) {
        if (self.clicktype != clicktypePass) {
            self.clicktype =clicktypeUser;
            return;
        }
        self.clicktype=clicktypeUser;
        [self notCoverOwlEyes];
    }else{
        if (self.clicktype == clicktypePass)
        {
            return;
        }
        self.clicktype = clicktypePass;
        [self coverOwlEyes];
    }

}
///限制文本输入框只输入英文和数字的方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSUInteger strlength = textField.text.length-range.length+string.length;
    
    if (strlength<=30 && [self filtrationTextFieldText:string textField:textField]) {
        return YES;
    }else{
        return NO;
    }
}
- (BOOL)filtrationTextFieldText:(NSString*)str textField:(UITextField*)textField{
    BOOL result = YES;
    NSCharacterSet* tmpSet;
    if (textField == self.accountTextField) {
        tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM"];
   
    int i = 0;
    while (i<str.length) {
        NSString* string = [str substringWithRange:NSMakeRange(i, 1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length == 0) {
            result = NO;
            break;
        }
        i++;
        
        
    }}
    if (textField == self.passwordTextFiled) {
        result =YES;
    }
    return result;
}

#pragma mark 登录按钮的回调方法
- (void)loginBtnIsClicked{
    self.loginView.layer.borderWidth = 0;
    self.leftHandImageView.hidden = YES;
    self.rightHandImageView.hidden = YES;
    self.loginBtnClickedBlock();
}
@end
